In [4]:
import numpy as np
import tensorflow as tf


Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz

In [12]:
x = tf.constant([[2, 2]])
y = tf.constant([[1], [3]])

with tf.Session() as sess:
    result = sess.run(tf.matmul(x, y))
    
print(result)


[[8]]

you can evaluate the result with foo.eval().


In [15]:
x = tf.Variable([1, 2])
a = tf.constant([3, 3])

with tf.Session() as sess:
    x.initializer.run()
    sub = tf.sub(x, a)
    result = sub.eval()
    
print(result)


[-2 -1]

In [22]:
import math

In [25]:
state = tf.Variable(0, name='Ge')
one = tf.constant(1)

new_state = tf.add(state, one)
update = tf.assign(state, new_state)
init_op = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(state))
    
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))


0
1
2
3

Now implement something similar.


In [31]:
some_state = tf.Variable(15, name="awesome")
two = tf.constant(2)

update = tf.assign(some_state, tf.mul(two, some_state))

with tf.Session() as sess:
    some_state.initializer.run()
    
    for _ in range(5):
        sess.run(update)
        print(update.eval())
        
        # eval executes the update twice. Same with `run`.
        print(update.eval())


60
120
480
960
3840
7680
30720
61440
245760
491520

In [37]:
input1 = tf.constant([3.0])
input2 = tf.constant([2.0])
input3 = tf.constant([5.0])
intermed = tf.add(input2, input3)
mul = tf.mul(input1, intermed)

with tf.Session() as sess:
  result = sess.run([mul, intermed])
  print(result)


[array([ 21.], dtype=float32), array([ 7.], dtype=float32)]

some explorations


In [45]:
import matplotlib.pyplot as plt
%matplotlib inline

In [66]:
initial = tf.truncated_normal([1000], stddev=0.1)
with tf.Session() as sess:
    result = initial.eval()
    # print(result)
    plt.plot(result)
    plt.title('Showing the raw initialized data')
    plt.show()
    
    plt.hist(result, np.linspace(-0.2, 0.2, 11), 
             ec='none', rwidth=0.9, color='#47c0fc')
    plt.xlim(-0.3, 0.3)
    plt.ylim(0, 200)



In [58]:



Out[58]:
array([-0.2 , -0.16, -0.12, -0.08, -0.04,  0.  ,  0.04,  0.08,  0.12,  0.16])

In [ ]: